package com.ims.core.doc.controller;

import java.math.BigDecimal;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ims.common.exception.ServiceException;
import com.ims.core.doc.domain.TScSettlebillRequest;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ims.common.annotation.Log;
import com.ims.common.core.controller.BaseController;
import com.ims.common.core.domain.AjaxResult;
import com.ims.common.enums.BusinessType;
import com.ims.core.doc.domain.TScSettlebill;
import com.ims.core.doc.service.ITScSettlebillService;
import com.ims.common.utils.poi.ExcelUtil;
import com.ims.common.core.page.TableDataInfo;

/**
 * 结算单Controller
 *
 * @author IMS
 * @date 2024-05-30
 */
@RestController
@RequestMapping("/doc/settlebill")
public class TScSettlebillController extends BaseController
{
    @Autowired
    private ITScSettlebillService tScSettlebillService;

    /**
     * 查询结算单列表
     */
    @PreAuthorize("@ss.hasPermi('doc:settlebill:list')")
    @GetMapping("/list")
    public TableDataInfo list(TScSettlebill tScSettlebill)
    {
        startPage();
        List<TScSettlebill> list = tScSettlebillService.selectTScSettlebillList(tScSettlebill);
        return getDataTable(list);
    }

    /**
     * 导出结算单列表
     */
    @PreAuthorize("@ss.hasPermi('doc:settlebill:export')")
    @Log(title = "结算单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TScSettlebill tScSettlebill)
    {
        List<TScSettlebill> list = tScSettlebillService.selectTScSettlebillList(tScSettlebill);
        ExcelUtil<TScSettlebill> util = new ExcelUtil<TScSettlebill>(TScSettlebill.class);
        util.exportExcel(response, list, "结算单数据");
    }

    /**
     * 获取结算单详细信息
     */
    @PreAuthorize("@ss.hasPermi('doc:settlebill:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(tScSettlebillService.selectTScSettlebillById(id));
    }

    /**
     * 获取结算单详细信息(settleId)
     */
    @PreAuthorize("@ss.hasPermi('doc:settlebill:query')")
    @GetMapping(value = "/getBySettleId/{settleId}")
    public AjaxResult getInfoBySettleId(@PathVariable("settleId") String settleId)
    {
        return success(tScSettlebillService.selectTScSettlebillBySettleId(settleId));
    }


    /**
     * 获取结算单详细信息(settleId) 结算开票页面用
     */
    @PreAuthorize("@ss.hasPermi('doc:settlebill:query')")
    @GetMapping(value = "/getByIdForInvoice/{id}")
    public AjaxResult getInfoByIdForInvoice(@PathVariable("id") Long id)
    {
        return success(tScSettlebillService.selectTScSettlebillByIdForInvoice(id));
    }

    /**
     * 获取结算单详细信息(settleId) 结算合并开票页面用
     */
    @PreAuthorize("@ss.hasPermi('doc:settlebill:query')")
    @GetMapping(value = "/getByIdsForMergeInvoice/{ids}")
    public AjaxResult getByIdsForMergeInvoice(@PathVariable("ids") Long[] ids)
    {
        return success(tScSettlebillService.selectTScSettlebillByIdForMergeInvoice(ids));
    }


    /**
     * 新增结算单
     */
    @PreAuthorize("@ss.hasPermi('doc:settlebill:add')")
    @Log(title = "结算单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TScSettlebill tScSettlebill)
    {
        return toAjax(tScSettlebillService.insertTScSettlebill(tScSettlebill));
    }

    /**
     * 修改结算单
     */
    @PreAuthorize("@ss.hasPermi('doc:settlebill:edit')")
    @Log(title = "结算单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TScSettlebill tScSettlebill)
    {
        return toAjax(tScSettlebillService.updateTScSettlebill(tScSettlebill));
    }

    /**
     * 保存拆分为待开发票
     */
    @PreAuthorize("@ss.hasPermi('doc:settlebill:edit')")
    @Log(title = "结算单", businessType = BusinessType.UPDATE)
    @PutMapping("/saveAsSalInvoice")
    public AjaxResult saveAsSalInvoice(@RequestBody TScSettlebillRequest tScSettlebillRequest)
    {
        tScSettlebillService.saveAsSalInvoice(tScSettlebillRequest);
        return success();
    }

    /**
     * 保存合并为待开发票
     */
    @PreAuthorize("@ss.hasPermi('doc:settlebill:edit')")
    @Log(title = "结算单", businessType = BusinessType.UPDATE)
    @PutMapping("/saveAsMergeSalInvoice")
    public AjaxResult saveAsMergeSalInvoice(@RequestBody TScSettlebillRequest tScSettlebillRequest)
    {
        tScSettlebillService.saveAsMergeSalInvoice(tScSettlebillRequest);
        return success();
    }

    /**
     * 删除结算单
     */
    @PreAuthorize("@ss.hasPermi('doc:settlebill:remove')")
    @Log(title = "结算单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tScSettlebillService.deleteTScSettlebillByIds(ids));
    }

    /**
     * 结算单生成待开
     */
    // @PreAuthorize("@ss.hasPermi('doc:settlebill:remove')")
    @Log(title = "结算单生成待开", businessType = BusinessType.UPDATE)
	@PutMapping("/doCreateInvoice/{ids}")
    public AjaxResult doCreateInvoice(@PathVariable Long[] ids)
    {
        tScSettlebillService.createInvoiceBySettlebillIds(ids);
        return success();
    }
}
